<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            思路：先找到最高的那根柱子，然后从左往右，从右往左，找到左边最高柱子
            */
            var trap = function (height) {
                let maxIndex = 0
                let maxHeight = 0
                let result = 0
                for (let i = 0; i < height.length; i++) {
                    if (height[i] > maxHeight) {
                        maxHeight = height[i]
                        maxIndex = i
                    }
                }
                //从左往右遍历
                let minLeftHeight = 0
                for (let i = 0; i < maxIndex; i++) {
                    minLeftHeight = Math.max(minLeftHeight, height[i])
                    result += minLeftHeight - height[i]
                }
                let minRightHeight = 0
                for (let i = height.length - 1; i > maxIndex; i--) {
                    minRightHeight = Math.max(minRightHeight, height[i])
                    result += minRightHeight - height[i]
                }
                return result
            }
        </script>
    </body>
</html>
